﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports utilitiesLibrary

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class store
    Inherits System.Web.Services.WebService
    Dim objDB As DBConnect
    Dim sql As String
    Dim ds As New DataSet

    <WebMethod(EnableSession:=True)> _
    Public Function getDepartments() As DataSet
        objDB = Session("db")

        sql = "SELECT * FROM mall_dept"
        ds = objDB.getDataSet(sql)

        Return ds
    End Function

    <WebMethod(EnableSession:=True)> _
    Public Function getProductCatalog(ByVal DeptNo As Object) As DataSet
        objDB = Session("db")

        Try
            sql = "SELECT ProductNumber, Description, Price, URL FROM mall_product WHERE DepartmentNumber=" & DeptNo & " AND QuantityOnHand > 0"
            ds = objDB.getDataSet(sql)
        Catch ex As Exception
            'if it goes here, not a valid dept num
        End Try

        Return ds
    End Function

    <WebMethod(EnableSession:=True)> _
    Public Function updateQOH(ByVal ProdNo As Object, ByVal Quantity As Integer) As Integer
        Dim oldOnHand As Integer
        Dim newOnHand As Integer
        Dim recordsModified As Integer = 0

        objDB = Session("db")

        Try
            sql = "SELECT * FROM mall_product WHERE ProductNumber=" & ProdNo
            objDB.getDataSet(sql)
            oldOnHand = objDB.getField("QuantityOnHand", 0)
            newOnHand = oldOnHand - Quantity
            sql = "UPDATE mall_product SET QuantityOnHand=" & newOnHand & " WHERE ProductNumber=" & ProdNo
            objDB.doUpdate(sql)
            recordsModified = 1
        Catch ex As Exception
            recordsModified = 0
        End Try


        Return recordsModified
    End Function
End Class